package com.android.kotlincoroutinemvvmdemo
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.widget.LinearLayoutCompat
import com.android.kotlincoroutinemvvmdemo.base.BaseActivity
import com.android.kotlincoroutinemvvmdemo.databinding.ActivityWebViewBinding
import com.android.kotlincoroutinemvvmdemo.webviewevent.JavaScriptObject

class WebViewActivity : BaseActivity<ActivityWebViewBinding>() {

    private lateinit var mWebView: WebView

    override fun getLayoutId() = R.layout.activity_web_view

    @SuppressLint("SetJavaScriptEnabled")
    override fun init(savedInstanceState: Bundle?) {
        mWebView = WebView(this)

        val webSettings = mWebView.settings
        webSettings.apply {
            //支持缩放
            builtInZoomControls = true
            displayZoomControls = true
            //支持js
            javaScriptEnabled = true
            //调整图片到合适的大小
            useWideViewPort = true
            //关闭缓存
            cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
            //可以访问文件
            allowFileAccess = true
            //支持通过js打开新窗口
            javaScriptCanOpenWindowsAutomatically = true
            //缩放值屏幕的大小
            loadWithOverviewMode = true
            //支持自动加载图片
            loadsImagesAutomatically = true
            //解决加载某些图片失败
            mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
        mWebView.apply {
            scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY //滚动条直接覆盖在网页上,不占用空间.
            addJavascriptInterface(JavaScriptObject(this@WebViewActivity),"baseWJ")
            //WebChromeClient用于处理js对话框、图标、页面图标
            webChromeClient = object : WebChromeClient() {
                override fun onProgressChanged(view: WebView?, newProgress: Int) {
                    //进度条变化
                    super.onProgressChanged(view, newProgress)
                }
            }
            //WebViewClient用于处理WebView的控制问题，比如:加载、关闭、错误处理
            webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {
                    return super.shouldOverrideUrlLoading(view, request)
                }
            }
            loadUrl("file:///android_asset/hpi0.html")
        }

        mBinding.btnShowAlert.setOnClickListener {
            //直接调用js中的方法
            mWebView.loadUrl("javascript:showAlert()")
        }

        mBinding.rootView.addView(
            mWebView,
            LinearLayoutCompat.LayoutParams(
                LinearLayoutCompat.LayoutParams.MATCH_PARENT,
                LinearLayoutCompat.LayoutParams.MATCH_PARENT
            )
        )
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (KeyEvent.KEYCODE_BACK == keyCode && mWebView.canGoBack()) {
            mWebView.goBack()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding.rootView.removeView(mWebView)
    }
}